/* $Id: ContestUserEntry.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.datatypes;

import java.util.Date;
import javax.cache.Cache;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import alt.djudge.frontend.server.models.CacheModel;
import alt.djudge.frontend.shared.dto.admin.ContestUserEntryDTO;
import alt.djudge.frontend.shared.dto.admin.AdminDTOEntry;


@PersistenceCapable(detachable="true")
public class ContestUserEntry extends AbstractEntry
{
	private static final long serialVersionUID = 1L;
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long id;
	
	@Override
	public Long getId()
	{
		return id;
	}
	
	@Override
	public void setId(Long id)
	{
		this.id = id;
	}
	
	@Persistent
	private Long contestId;
	
	@Persistent
	private Long userId;

	@Persistent
	private String name;

	@Persistent
	private String role;

	@Persistent
	private Date startTime = new Date();
	
	@Persistent
	private Boolean virtual = false;

	public ContestUserEntry()
	{
		
	}

	public ContestUserEntry(UserEntry user)
	{
		name = user.getUsername();
		role = user.getRole();
		setVirtual(user.getVirtual());
	}	
	
	public ContestUserEntry(Long contestId, Long userId)
	{
		this.contestId = contestId;
		this.userId = userId;
	}	
	
	@Override
	public void clearCacheCustom()
	{
		Cache cache = CacheModel.getStaticCache();
		String keyContestUser = CacheModel.getContestUserEntryKey(contestId.toString(), userId.toString());
		cache.remove(keyContestUser);
		String monitorKey = CacheModel.getMonitorKey(getContestId());
		cache.remove(monitorKey);
	}	
	
	public void setContestId(Long contestId)
	{
		this.contestId = contestId;
	}

	public Long getContestId()
	{
		return contestId;
	}

	public void setUserId(Long userId)
	{
		this.userId = userId;
	}

	public Long getUserId()
	{
		return userId;
	}

	public void setStartTime(Date startTime)
	{
		this.startTime = startTime;
	}

	public Date getStartTime()
	{
		return startTime;
	}

	public void setRole(String role)
	{
		this.role = role;
	}

	public String getRole()
	{
		return role;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}
	
	public boolean canSubmit()
	{
		return role.startsWith("user") || role.startsWith("judge") || role.startsWith("admin");
	}
	
	public boolean isScored()
	{
		return canSubmit();
	}
	
	public boolean isJudge()
	{
		return role.startsWith("admin") || role.startsWith("judge");
	}

	public void setVirtual(Boolean virtual)
	{
		this.virtual = virtual;
	}

	public Boolean getVirtual()
	{
		return virtual != null ? virtual : false;
	}
	
	@Override
	public void fromDTO(AdminDTOEntry dtoEntry)
	{
		setContestId(Long.parseLong(dtoEntry.getFieldValue(1)));
		setUserId(Long.parseLong(dtoEntry.getFieldValue(2)));
		setName(dtoEntry.getFieldValue(3));
		setRole(dtoEntry.getFieldValue(4));
	}

	@Override
	public AdminDTOEntry ownAdminDTO()
	{
		String[] data = new String[] {
			getId().toString(),
			getContestId().toString(),
			getUserId().toString(),
			getName(),
			getRole(),
		};
		return new ContestUserEntryDTO(data);
	}
}
